import { checkActor } from "../utils/mirUtil";
import { changeDamageCut, randomInRange } from "../utils/utls";
import { getCustomSkillManage, getSkillManage, getSkillName, getSpecialMonManage } from "../系统功能/GlobalObjectManage";
import { ActorNkeyEnum, MaxPercentNum } from "../配置/基础配置";
import { checkAddShieldDamage, getShieldPower, getStrengSkillPower } from "../配置/强化技能";
import { PlayerDamageManage } from "./PlayerDamageManage";



export interface onDamageChange {
    (ActorObject: TActor, ADamageSource: TActor, Tag: number,
        SkillID: number, SkillLevel: number, Value: number): Array<DamageChangeData>;
}

export class DamageChangeData {
    //伤害改变值 加法
    addPercent: number;
    //改变结果伤害
    add: number;
    constructor() {
        this.add = 0;
        this.addPercent = 0;
    }
}

export class DamageChangeManage {
    playerDamageManage: PlayerDamageManage

    constructor() {
        this.playerDamageManage = new PlayerDamageManage();
    }

    //伤害触发
    onMonitorDamageEx(ActorObject: TActor, ADamageSource: TActor,
        Tag: number, SkillID: number, SkillLevel: number, Value: number): number {
        if (!checkActor(ActorObject)) {
            return Value;
        }
        if (!checkActor(ADamageSource)) {
            return Value;
        }
        let tag = ActorObject.GetNVar(ActorNkeyEnum.NoDamageCountTag);
        if (tag == 1) {
            ActorObject.SetNVar(ActorNkeyEnum.NoDamageCountTag, 0)
            return Value
        }
        // console.log(ActorObject.Name + " " + Value)


        let list: Array<DamageChangeData> = new Array<DamageChangeData>();

        let skillName = getSkillName(SkillID)
        // console.log(`${skillName}  ${SkillID}`)

        //强化技能额外伤害
        if (skillName) {

            // console.log(skillName)
            let magic: TUserMagic = ADamageSource.FindSkill(skillName);
            if (magic) {
                Value = getSkillManage().onMonitorDamageEx(ActorObject, ADamageSource, Tag, magic, Value)
            }


            let num: number = getStrengSkillPower(magic);
            if (num > 0) {
                let data: DamageChangeData = new DamageChangeData();
                data.addPercent = num;
                list.push(data)
            }
        }



        //攻击元素
        let add = ADamageSource.GetNVar(ActorNkeyEnum.DamageAdd) || 0
        let addper = ADamageSource.GetNVar(ActorNkeyEnum.DamageAddPer) || 0
        // console.log(add + " " + addper)
        if (add || addper) {
            let data: DamageChangeData = new DamageChangeData();
            data.add = add;
            data.addPercent = addper;
            list.push(data)
        }

        //防御元素
        let cut = ActorObject.GetNVar(ActorNkeyEnum.DamageCut) || 0
        let cutper = ActorObject.GetNVar(ActorNkeyEnum.DamageCutPer) || 0
        if (cut || cutper) {
            let data: DamageChangeData = new DamageChangeData();
            data.add = -cut;
            data.addPercent = -cutper;
            list.push(data)
        }

        //对怪物伤害
        if (!ActorObject.IsPlayer()) {
            //攻击元素
            let add = ADamageSource.GetNVar(ActorNkeyEnum.MonDamageAdd) || 0
            let addper = ADamageSource.GetNVar(ActorNkeyEnum.MonDamageAddPer) || 0
            if (add || addper) {
                let data: DamageChangeData = new DamageChangeData();
                data.add = add;
                data.addPercent = addper;
                list.push(data)
            }
        }

        //对怪物减伤
        if (!ADamageSource.IsPlayer()) {
            //防御元素
            let cut = ActorObject.GetNVar(ActorNkeyEnum.MonDamageCut) || 0
            let cutper = ActorObject.GetNVar(ActorNkeyEnum.MonDamageCutPer) || 0
            if (cut || cutper) {
                let data: DamageChangeData = new DamageChangeData();
                data.add = -cut;
                data.addPercent = -cutper;
                list.push(data)
            }
        }



        add = ADamageSource.GetNVar(ActorNkeyEnum.ShieldDamage) || 0
        addper = ADamageSource.GetNVar(ActorNkeyEnum.ShieldDamagePer) || 0
        if (ActorObject.CheckState(11)) {
            //魔法盾减免
            let magic: TUserMagic = ActorObject.FindSkill("魔法盾");
            let num: number = getShieldPower(magic);

            if (num > 0) {
                let data: DamageChangeData = new DamageChangeData();
                data.addPercent = -num;
                list.push(data)
            }



            //对魔法盾额外伤害的技能
            let data: DamageChangeData = checkAddShieldDamage(ActorObject, ADamageSource, skillName)
            if (data) {
                list.push(data)
            }

            //魔法盾额外伤害的元素
            if (add || addper) {
                let data: DamageChangeData = new DamageChangeData();
                data.add = add;
                data.addPercent = addper;
                list.push(data)
            }
        }





        let result: Array<DamageChangeData>;

        //技能触发
        result = getCustomSkillManage().onSkillDamage(ActorObject, ADamageSource, Tag, SkillID, SkillLevel, Value);
        if (result && result.length > 0) {
            list = list.concat(result);
        }

        //人物攻击
        // result=this.playerDamageManage.onPlayerAttack(ActorObject,ADamageSource,Tag,SkillID,SkillLevel,Value);
        // if(result&&result.length>0)
        // {
        //     list=list.concat(result);
        // }

        // //人物下属攻击
        // result=this.playerDamageManage.onSlaveAttack(ActorObject,ADamageSource,Tag,SkillID,SkillLevel,Value);
        // if(result&&result.length>0)
        // {
        //     list=list.concat(result);
        // }

        //怪物攻击
        result = getSpecialMonManage().onMonAttack(ActorObject, ADamageSource, Tag, SkillID, SkillLevel, Value);
        if (result && result.length > 0) {
            list = list.concat(result);
        }

        //人物被攻击
        // result = this.playerDamageManage.onPlayerStruck(ActorObject, ADamageSource, Tag, SkillID, SkillLevel, Value);
        // if (result && result.length > 0) {
        //     list = list.concat(result);
        // }

        // //人物下属攻击
        // result=this.playerDamageManage.onSlaveStruck(ActorObject,ADamageSource,Tag,SkillID,SkillLevel,Value);
        // if(result&&result.length>0)
        // {
        //     list=list.concat(result);
        // }

        //怪物攻击
        result = getSpecialMonManage().onMonStruck(ActorObject, ADamageSource, Tag, SkillID, SkillLevel, Value);
        if (result && result.length > 0) {
            list = list.concat(result);
        }

        //统计伤害
        Value = sumDamage(Value, list)
        //调用最终伤害触发 给仇恨系统使用
        getSpecialMonManage().onMonDamage(ActorObject, ADamageSource, Value);

        //计算伤害反弹
        let back = ActorObject.GetNVar(ActorNkeyEnum.DamageBack) || 0;
        let backPer = ActorObject.GetNVar(ActorNkeyEnum.DamageBackPer) || 0;
        let damage = Math.floor(backPer * Value / MaxPercentNum + back / 10)
        // console.log(`${ActorObject.Name}反弹伤害${damage}  ${back} ${backPer} ${Value}`)

        if (damage > 0) {
            getSpecialMonManage().onMonDamage(ADamageSource, ActorObject, damage);
            ADamageSource.SetNVar(ActorNkeyEnum.NoDamageCountTag, 1)
            // console.log(`${ActorObject.Name}反弹${ADamageSource.Name} 造成${damage}伤害`)
            ActorObject.Damage(ADamageSource, damage)
        }




        //伤害提示
        if (ActorObject.IsPlayer()) {
            let Player: TPlayObject = ActorObject as TPlayObject
            let 伤害提示 = Player.V.伤害提示
            if (伤害提示) {
                let str = `收到来自${ADamageSource?.Name || ''}${Value}的伤害,技能ID ${SkillID},${skillName ? `使用技能 ${skillName}` : ''}`;
                Player.SendMessage(str, 1)
            }
            let damageLogStart = Player.R.damageLogStart
            if (damageLogStart > 0) {
                // let tick = GameLib.TickCount;
                // if (tick - damageLogStart >= 100000) {
                //     Player.R.damageLogStart = 0
                //     Player.R.damageLogList = []
                // }
                // else {
                Player.R.damageLogSum += Value
                // }
            }
        }
        //攻击提示
        if (ADamageSource.IsPlayer()) {
            let Player: TPlayObject = ADamageSource as TPlayObject
            let 伤害提示 = Player.V.伤害提示
            if (伤害提示) {
                let str = `攻击${ActorObject?.Name || ''}造成${Value}的伤害,技能ID ${SkillID},${skillName ? `使用技能 ${skillName}` : ''}`;
                Player.SendMessage(str, 1)
            }
        }

        //伤害反弹提示
        // if (ADamageSource.IsPlayer() && damage > 0) {
        //     let Player: TPlayObject = ADamageSource as TPlayObject
        //     let 伤害提示 = Player.V.伤害提示
        //     if (伤害提示) {
        //         Player.SendMessage(`收到来自${ActorObject.Name}的${damage}点反弹伤害`, 1)
        //     }
        // }

        //自动上毒
        if (!ActorObject.IsPlayer() && ADamageSource.IsPlayer()) {
            if (!ActorObject.CheckState(0)) {
                let flag = false;
                let dValue = 1;
                let magic: TUserMagic = ADamageSource.FindSkill("施毒术")
                if (magic) {
                    if (skillName == "灵魂火符") {
                        let magic2: TUserMagic = ADamageSource.FindSkill(skillName)
                        if (magic.Strengthen >= 10 && magic2.Strengthen >= 10) {
                            let dc = randomInRange(ADamageSource.DCMin, ADamageSource.DCMax + 1);
                            let power = getStrengSkillPower(magic);
                            dValue = Math.floor((dc / 4 + 5) * (1 + power / MaxPercentNum))
                            flag = true
                        }
                    }
                    if (flag) {
                        ActorObject.SetState(1, 120, 8)
                        ActorObject.SetState(0, 120, dValue)
                    }
                    else {
                        ActorObject.SetState(0, 120, dValue);
                    }
                }

            }
        }

        return Value
    }
}

//汇总伤害
function sumDamage(value: number, changeList: Array<DamageChangeData>): number {
    if (!changeList || changeList.length == 0) {
        return value;
    }
    let sumPercent = 0
    let sumResult = 0;
    for (let i = 0; i < changeList.length; i++) {
        let item = changeList[i];
        sumPercent += item.addPercent;
        sumResult += item.add / 10;
    }
    // console.log(sumPercent + " " + sumResult + " " + value)
    if (sumPercent >= 0) {
        value = Math.floor(value * (1 + sumPercent / MaxPercentNum) + sumResult);
    }
    else {
        let curPer = changeDamageCut(-sumPercent);
        value = Math.floor(value * (1 - curPer) + sumResult);
    }

    if (value < 1) {
        value = 1;
    }
    return value
}